﻿@{
    var grid = Html.DevExpress().GridView(
    settings =>
    {
        settings.Name = "gvUser";
        settings.KeyFieldName = "ID";
        settings.CallbackRouteValues = new { Controller = "User", Action = "UserRowPartial" };
        settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "User", Action = "UpdateUser" };
        settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "User", Action = "AddUser" };
        settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "User", Action = "DeleteUser" };
        settings.CustomBindingRouteValuesCollection.Add(GridViewOperationType.Paging, new { Controller = "User", Action = "UserPagingRowPartial" });
        settings.CustomBindingRouteValuesCollection.Add(GridViewOperationType.Filtering, new { Controller = "User", Action = "UserFilteringRowPartial" });
        settings.CustomBindingRouteValuesCollection.Add(GridViewOperationType.Sorting, new { Controller = "User", Action = "UserSortingRowPartial" });
        settings.CustomActionRouteValues = new { Controller = "User", Action = "ApplyFilterExpressionPartial" };

        settings.SettingsEditing.Mode = GridViewEditingMode.PopupEditForm;
        settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;
        settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.WindowCenter;
        settings.SettingsPopup.EditForm.Modal = true;
        settings.CommandColumn.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
        settings.SettingsBehavior.ConfirmDelete = true;
        settings.SettingsPopup.EditForm.Width = new System.Web.UI.WebControls.Unit(900);
        settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);
        settings.SettingsText.PopupEditFormCaption = "User Management";
        settings.ControlStyle.CssClass = "edit-form";
        settings.SettingsPager.PageSize = Session["PageSize"] != null ? (int)Session["PageSize"] : 10;
        settings.SettingsBehavior.AllowFocusedRow = true;
        settings.CommandColumn.Visible = true;
        settings.CommandColumn.ShowSelectCheckbox = true;
        settings.ClientSideEvents.Init = @"function(s,e){ 
            s.UnselectAllRowsOnPage();
        }";
        settings.ClientSideEvents.BeginCallback = "gvUserBeginCallback";
        settings.SetEditFormTemplateContent(e =>
        {
            var model = new UserViewModel();
            var userID = DataBinder.Eval(e.DataItem, "ID");
            if (userID != null) { 
                var user = new ThinhTien.Data.Repository.UserRepository();
                model = user.GetUserDetail(new Guid(userID.ToString()));
            }
            
            
            Html.RenderPartial("UserFormPartial", model);
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "UserName";
            column.Caption = "User Name";
            column.Settings.AutoFilterCondition = AutoFilterCondition.Contains;
        });

        settings.Columns.Add(column =>
        {
            column.FieldName = "FullName";
            column.Caption = "Full Name";
            column.Settings.AutoFilterCondition = AutoFilterCondition.Contains;
        });
        
        settings.Columns.Add(column =>
        {
            column.FieldName = "Email";
            column.Settings.AutoFilterCondition = AutoFilterCondition.Contains;
        });
        
        settings.Columns.Add(column =>
        {
            column.FieldName = "PhoneNumber";
            column.Caption = "Extension Number";
            column.Settings.AutoFilterCondition = AutoFilterCondition.Contains;
        });

        var commandColumn = settings.Columns.Add("Action", "Action");
        commandColumn.Settings.AllowAutoFilter = DefaultBoolean.False;
        commandColumn.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
        commandColumn.Width = 1;
        commandColumn.SetDataItemTemplateContent(c =>
        {
            Html.DevExpress().Button(s =>
            {
                s.Name = string.Format("btnEdit_{0}", DataBinder.Eval(c.DataItem, "ID"));
                s.Text = "Edit";
                s.ClientSideEvents.Click = "function(s,e){ gvUser.StartEditRowByKey('" + DataBinder.Eval(c.DataItem, "ID") + "');}";
            }).GetHtml();
        });
       
        settings.ClientSideEvents.RowDblClick = "function(s,e){s.StartEditRowByKey(s.GetRowKey(e.visibleIndex));}";

        settings.Settings.ShowFilterRowMenu = settings.Settings.ShowFilterRow;
        settings.CommandColumn.ClearFilterButton.Visible = settings.Settings.ShowFilterRow;
        settings.SettingsPager.Position = System.Web.UI.WebControls.PagerPosition.Bottom;
        settings.SettingsPager.FirstPageButton.Visible = true;
        settings.SettingsPager.LastPageButton.Visible = true;
        settings.SettingsPager.PageSizeItemSettings.Visible = true;
        settings.SettingsPager.PageSizeItemSettings.Items = new string[] { "10", "20", "50" };

    });


    if (ViewData["EditError"] != null)
    {
        grid.SetEditErrorText((string)ViewData["EditError"]);
    }
    
    @grid.BindToCustomData(Model).GetHtml()
}
